//
// Created by zhupi on 2024/11/4.
//

#ifndef DEMO2_ADD_PROPERTES_DIALG_H
#define DEMO2_ADD_PROPERTES_DIALG_H


#include <QApplication>
#include <QComboBox>
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QRadioButton>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QGroupBox>
#include <QDebug>

class AddPropertyDialog : public QDialog {
Q_OBJECT

public:
    AddPropertyDialog(QWidget *parent = nullptr) : QDialog(parent) {}
    void Initial() {
        setWindowTitle("Add Property");

        // 创建控件
        QLabel *nameLabel = new QLabel("Name:");
        nameEdit = new QLineEdit;

        variableRadio = new QRadioButton("Variable");
        separatorRadio = new QRadioButton("Separator");
        arrayIndexVariableRadio = new QRadioButton("ArrayIndexVariable");
        postProcessingVariableRadio = new QRadioButton("PostProcessingVariable");

        QLabel *unitTypeLabel = new QLabel("Unit Type:");
        unitTypeCombo = new QComboBox;
        unitTypeCombo->addItem("None");
        unitTypeCombo->addItem("Length");
        unitTypeCombo->addItem("Time");

        QLabel *unitsLabel = new QLabel("Units:");
        unitsCombo = new QComboBox;
        unitsCombo->addItem("");

        QLabel *valueLabel = new QLabel("Value:");
        valueEdit = new QLineEdit;
        valueEdit->setText("0");

        QLabel *infoLabel = new QLabel("Enter initial value into Value field. This should be a number, variable, or expression.\n"
                                       "Referenced project variables should be prefixed with a '$': Examples: 22.4pF,\n"
                                       "$C1, 2*cos($x).");

        // 布局设置
        QGridLayout *layout = new QGridLayout;
        layout->addWidget(nameLabel, 0, 0);
        layout->addWidget(nameEdit, 0, 1, 1, 4);

        layout->addWidget(variableRadio, 0, 5);
        layout->addWidget(separatorRadio, 0, 6);
        layout->addWidget(arrayIndexVariableRadio, 0, 7);
        layout->addWidget(postProcessingVariableRadio, 0, 8);

        layout->addWidget(unitTypeLabel, 1, 0);
        layout->addWidget(unitTypeCombo, 1, 1, 1, 2);
        layout->addWidget(unitsLabel, 1, 3);
        layout->addWidget(unitsCombo, 1, 4, 1, 5);

        layout->addWidget(valueLabel, 2, 0);
        layout->addWidget(valueEdit, 2, 1, 1, 8);

        layout->addWidget(infoLabel, 3, 0, 1, 9);

        QPushButton *okButton = new QPushButton("OK");
        QPushButton *cancelButton = new QPushButton("Cancel");
        QHBoxLayout *buttonLayout = new QHBoxLayout;
        buttonLayout->addStretch();
        buttonLayout->addWidget(okButton);
        buttonLayout->addWidget(cancelButton);

        layout->addLayout(buttonLayout, 4, 0, 1, 9, Qt::AlignRight);

        setLayout(layout);

        // 连接信号和槽
        connect(variableRadio, &QRadioButton::toggled, this, &AddPropertyDialog::updateUI);
        connect(separatorRadio, &QRadioButton::toggled, this, &AddPropertyDialog::updateUI);
        connect(arrayIndexVariableRadio, &QRadioButton::toggled, this, &AddPropertyDialog::updateUI);
        connect(postProcessingVariableRadio, &QRadioButton::toggled, this, &AddPropertyDialog::updateUI);
        connect(okButton, &QPushButton::clicked, this, &QDialog::accept);
        connect(cancelButton, &QPushButton::clicked, this, &QDialog::reject);

        // 初始界面设置
        variableRadio->setChecked(true);
        updateUI();
    }

private slots:
    void updateUI() {
        bool isVariable = variableRadio->isChecked();
        bool isSeparator = separatorRadio->isChecked();
        bool isArrayIndexVariable = arrayIndexVariableRadio->isChecked();

        // 更新组件的可见性
        unitTypeCombo->setVisible(isVariable || isArrayIndexVariable);
        unitsCombo->setVisible(isVariable || isArrayIndexVariable);
        valueEdit->setVisible(isVariable || isArrayIndexVariable || postProcessingVariableRadio->isChecked());
        infoLabel->setVisible(isVariable || postProcessingVariableRadio->isChecked());
    }

private:
    QLineEdit *nameEdit;
    QRadioButton *variableRadio;
    QRadioButton *separatorRadio;
    QRadioButton *arrayIndexVariableRadio;
    QRadioButton *postProcessingVariableRadio;
    QComboBox *unitTypeCombo;
    QComboBox *unitsCombo;
    QLineEdit *valueEdit;
    QLabel *infoLabel;
};

#endif //DEMO2_ADD_PROPERTES_DIALG_H
